<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use Route;
use Illuminate\Support\Facades\DB;

class RoleLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $r = $request->except([]);
        $parmas = [];
        foreach($r as $k => $v)
        {
            if( $request->hasFile($k)) continue ;
            $parmas[$k] = $v;
        }
        //dd( $parmas );
        $data = [
            'user_id'		=>	Auth::user()->id,
            'user_name'	    =>	Auth::user()->name,
            'route'			=>	Route::currentRouteName(),
            'method'		=>	($request->isMethod('post') ? 'post' : 'get'),
            'params'		=>	$parmas ? json_encode($parmas) : '',
            'ip'			=>	$request->ip(),
            'time'			=>	date('Y-m-d H:i:s',time()),
        ];
        DB::table('users_log')->insert($data);
        return $next($request);
    }
}
